// pages/index/community_organization/community_organization_join/community_organization_join.js
let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    result: [],
    isClickPublish: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    let item = JSON.parse(options.item);
    this.setData({
      circleDetail: item,
      circleId: item.circle_id,
      theme: app.globalData.theme,
      type: options.type
    });
    this.getArticleList();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (this.data.isClickPublish) {
      app.globalData.pageNumber = 1;
      this.setData({
        result: [],
        isClickPublish: false
      });
      this.getArticleList();
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {
    app.globalData.pageNumber = 1;
    app.globalData.pageTotalNumber = 0;
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    app.globalData.pageNumber++;
    this.setData({
      mpLoadingShow: true,
      mpLoadingTips: "加载中..."
    });
    if (app.globalData.pageNumber <= app.globalData.pageTotalNumber) {
      this.getArticleList();
    } else {
      setTimeout(() => {
        this.setData({
          mpLoadingTips: "暂无数据"
        });
      }, 500);
      setTimeout(() => {
        this.setData({
          mpLoadingShow: false
        });
      }, 1000);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  /**
   * 加入圈子
   */
  joinOrganization: function() {
    app.wxGetRequest("/circle/join", {
      circle_id: this.data.circleId,
      user_id: app.globalData.userInfo["user_id"]
    }).then(result => {
      result.data.data["is_injon"] = app.globalData.userInfo["user_id"];
      this.setData({
        circleDetail: result.data.data
      });
    });
  },
  /**
   * 退出圈子
   */
  exitOrganization: function() {
    let pages = getCurrentPages();
    let prevPage = pages[pages.length - 2];
    app.wxGetRequest("/circle/quit", {
      circle_id: this.data.circleId,
      user_id: app.globalData.userInfo["user_id"]
    }).then(result => {
      if (this.data.type == 0) {
        prevPage.setData({
          isClickExit: true
        });
        wx.navigateBack({
          delta: getCurrentPages()
        })
        return;
      }
      result.data.data["is_injon"] = null;
      this.setData({
        circleDetail: result.data.data
      });
    });
  },
  /**
   * 发布
   */
  publishArticle: function() {
    wx.navigateTo({
      url: '/pages/index/community_organization/add_article/add_article?id=' + this.data.circleId,
    })
  },
  /**
   * 文章详情
   */
  articleDetail: function(e) {
    let id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/index/community_organization/article_detail/article_detail?id=' + id,
    })
  },
  /**
   * 文章列表
   */
  getArticleList: function() {
    app.wxGetRequest("/circle/getArticleList", {
      circle_id: this.data.circleId,
      pageNumber: app.globalData.pageNumber,
      pageSize: app.globalData.pageSize
    }).then(result => {
      this.setData({
        result: this.data.result.concat(result.data.list),
        noData: result.data.totalRow == 0 ? true : false,
        mpLoadingShow: false
      });
      app.globalData.pageTotalNumber = result.data.totalPage;
    });
  }
})